package com.fromfuture.xuhuidoc.android.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.fromfuture.xuhuidoc.R;
import com.fromfuture.xuhuidoc.android.adapter.DocCircleChatListviewAdapter;
import com.fromfuture.xuhuidoc.android.bean.DocCircleChatMsgInfo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

public class DocCircleChatRoomActivity extends AppCompatActivity implements View.OnClickListener,Runnable{

    private DocCircleChatListviewAdapter adapter = null;

    private ListView listview;
    private Button btn_left,btn_right,btn_more;
    private EditText et_meg;
    private LinearLayout linearLayout_wjbl;

    private static final String HOST="https://sch.xmidware.com";
    private static final int PORT=9001;
    private Socket socket=null;
    private BufferedReader in = null;
    private PrintWriter out = null;
    private String content = "";
    //接收线程发送过来信息，并用TextView显示
    public Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
//            tv_msg.setText(content);
            adapter.addDataToAdapter(new DocCircleChatMsgInfo(null,content));
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doc_circle_chat_room);
        init();

        adapter = new DocCircleChatListviewAdapter(this);
        listview.setAdapter(adapter);
    }

    private void init(){
        findViewById();
    }

    private void findViewById(){
        listview = (ListView) findViewById(R.id.doc_circle_chat_listview);
        btn_left = (Button) findViewById(R.id.doc_circle_chat_btn_left);
        et_meg = (EditText) findViewById(R.id.doc_circle_chat_et_meg);
        btn_right = (Button) findViewById(R.id.doc_circle_chat_btn_right);
        btn_more= (Button) findViewById(R.id.doc_circle_chat_btn_jia);
        linearLayout_wjbl= (LinearLayout) findViewById(R.id.doc_circle_chat_wjbl);

        btn_left.setOnClickListener(this);
        btn_right.setOnClickListener(this);
        btn_more.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String msg = et_meg.getText().toString().trim();

        switch (v.getId()){
            case R.id.doc_circle_chat_btn_left:
                adapter.addDataToAdapter(new DocCircleChatMsgInfo(msg,null));
                adapter.notifyDataSetChanged();
                break;
            case R.id.doc_circle_chat_btn_right:
                adapter.addDataToAdapter(new DocCircleChatMsgInfo(null,msg));
                adapter.notifyDataSetChanged();
                break;
            case R.id.doc_circle_chat_btn_jia:
                if (linearLayout_wjbl.getVisibility()==View.GONE){
                    linearLayout_wjbl.setVisibility(View.VISIBLE);
                }else {
                    linearLayout_wjbl.setVisibility(View.GONE);
                }
                break;
        }
        listview.smoothScrollToPosition(listview.getCount() - 1);
        et_meg.setText("");
    }

    /**
     * 如果连接出现异常，弹出AlertDialog！
     */
    public void ShowDialog(String msg) {
        new AlertDialog.Builder(this).setTitle("notification").setMessage(msg)
                .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }).show();
    }

    private void soket(){

        try {
            socket = new Socket(HOST, PORT);
            in = new BufferedReader(new InputStreamReader(socket
                    .getInputStream()));
            out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
                    socket.getOutputStream())), true);
        } catch (IOException ex) {
            ex.printStackTrace();
            ShowDialog("login exception" + ex.getMessage());
        }

    }
    /**
     * 读取服务器发来的信息，并通过Handler发给UI线程
     */
    @Override
    public void run() {
        try {
            while (true) {
                if (!socket.isClosed()) {
                    if (socket.isConnected()) {
                        if (!socket.isInputShutdown()) {
                            if ((content = in.readLine()) != null) {
                                content += "\n";
                                mHandler.sendMessage(mHandler.obtainMessage());
                            } else {

                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
